package com.ruoyi.api.v1;

import cn.hutool.core.date.DateUtil;

import com.ruoyi.cinema.BO.FilmCinemaBO;
import com.ruoyi.cinema.domain.Cinema;
import com.ruoyi.cinema.domain.Film;
import com.ruoyi.cinema.mapper.CinemaMapper;
import com.ruoyi.cinema.service.ICinemaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController("v1_Cinema")
@RequestMapping(value = "api/v1/cinema")
public class CinemaController extends BaseController {
    @Autowired
    private ICinemaService cinemaService;
    @Autowired
    private CinemaMapper cinemaMapper;


    @GetMapping("/list")
    public TableDataInfo list(Cinema cinema)
    {
        startPage();
        List<Cinema> list = cinemaMapper.listForApi(cinema);
        return getDataTable(list);
    }

    @GetMapping("/listByFilm")
    public AjaxResult listByFilm(FilmCinemaBO filmCinemaBO){
        if(StringUtils.isEmpty(filmCinemaBO.getFilmId())){
            return AjaxResult.error(-1,"filmId 不能为空");
        }
        if(null != filmCinemaBO.getShowDate()){
            filmCinemaBO.setBeginShowDateTime(DateUtil.beginOfDay(filmCinemaBO.getShowDate()).toJdkDate());
            Long currentTime = System.currentTimeMillis();
            if(filmCinemaBO.getBeginShowDateTime().getTime() < currentTime){
                filmCinemaBO.setBeginShowDateTime(new Date(currentTime));
            }
            filmCinemaBO.setEndShowDateTime(DateUtil.endOfDay(filmCinemaBO.getShowDate()).toJdkDate());
        }
        List<Cinema> cinemaList = cinemaMapper.listByFilm(filmCinemaBO);
        return AjaxResult.success(cinemaList);
    }

    @GetMapping("/get")
    public AjaxResult getInfo(Cinema cinema) {
        return AjaxResult.success(cinemaService.selectCinemaById(cinema.getId()));
    }
}
